package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by larahy on 22/04/2014.
 */
public class Library {

    ArrayList<Book> books;
    ArrayList<Movie> movies;
    
    public Library (ArrayList<Book> b)  {books = b;}

    public Library (ArrayList<Book> b, ArrayList<Movie> m) {books = b; movies = m;}

    public ArrayList<Book> availableBooks() {
        ArrayList<Book> availableBooks = new ArrayList<Book>();

        for(Book book: books) {
            if (book.available) {
                availableBooks.add(book);
            }
        }
        return availableBooks;
    }

    public ArrayList<Movie> availableMovies() {
        ArrayList<Movie> availableMovies = new ArrayList<Movie>();

        for(Movie movie: movies) {
            if(movie.available) {
                availableMovies.add(movie);
            }
        }
        return availableMovies;
    }

    public Rentable findByTitle(String title) {
        for(Book book: books) {
            if(book.title.equals(title)) {
                return book;
            }
        }
        for(Movie movie: movies) {
            if(movie.title.equals(title)) {
                return movie;
            }
        }
        return null;
    }


    boolean exists(String title) {
        if (titleExists(title, books)) return true;
        if (titleExists(title, movies)) return true;
        return false;
    }

    private boolean titleExists(String title, ArrayList<? extends Rentable> rentables) {
        for(Rentable rentable: rentables) {
            if (rentable.title.equals(title)) {
                return true;
            }
        }
        return false;
    }

    boolean available(String title) {
        if (availableRentable(title, books)) return true;
        if (availableRentable(title, movies)) return true;
        return false;
    }

    private boolean availableRentable(String title, ArrayList<? extends Rentable> rentables) {
        for(Rentable rentable: rentables) {
            if (rentable.title.equals(title) && rentable.available) {
                return true;
            }
        }
        return false;
    }

    void addBook(String title) {
        books.add(new Book(title));
    }

//    void addMovie(String title) {
//        movies.add(new Movie(title));
//    }

    void checkout(String title) throws Exception {
        if (checkoutRentable(title, books)) return;
        if (checkoutRentable(title, movies)) return;

        throw new Exception("Title not available");
    }

    private boolean checkoutRentable(String title, ArrayList<? extends Rentable> rentables) {
        for(Rentable rentable: rentables) {
            if (rentable.title.equals(title) && rentable.available) {
                rentable.setAvailable(false);
                return true;
            }
        }
        return false;
    }

    void returnItem(String title) throws Exception {
        if (returnRentable(title, books)) return;
        if (returnRentable(title, movies)) return;


        throw new RuntimeException("That is not a valid item to return.");
    }

    private boolean returnRentable(String title, ArrayList<? extends Rentable> rentables) {
        for (Rentable rentable: rentables) {
            if (rentable.title.equals(title) && !rentable.available) {
                rentable.setAvailable(true);
                return true;
            }
        }
        return false;
    }


}
